// import Highlight from "react-highlight";
import { Light as Highlight } from "react-syntax-highlighter";
import { a11yDark as dark } from "react-syntax-highlighter/dist/esm/styles/prism";
import js from "react-syntax-highlighter/dist/esm/languages/hljs/javascript";
import bash from "react-syntax-highlighter/dist/cjs/languages/hljs/bash";
Highlight.registerLanguage("javascript", js);
Highlight.registerLanguage("bash", bash);
const Code = (props) => {
  return (
    <div>
      <div className="mb-4">安装</div>
      {props.frame === "react" && (
        <Highlight
          className="overflow-x-hidden rounded-lg mb-10"
          style={dark}
          language="bash"
        >
          npm install --save react-form-data-observe
        </Highlight>
      )}

      {props.frame === "vue" && (
        <Highlight
          className="overflow-x-hidden rounded-lg mb-10"
          style={dark}
          language="bash"
        >
          npm install --save vue-form-data-observe
        </Highlight>
      )}

      <div className="mb-4">代码示例</div>
      {props.frame === "react" && (
        <Highlight
          className="h-52 overflow-x-hidden rounded-lg"
          style={dark}
          language="javascript"
        >
          {`import { Button, Form, Input, Select, Space, Tooltip, Typography } from "antd";
import React from "react";
import { useState } from "react";
const { Option } = Select;
import { QuestionCircleOutlined } from "@ant-design/icons";
import useDataFormObserve from "../hooks/useDataFormObserve";
const App = () => {
const onFinish = (values) => {
    console.log("Received values of form: ", values);
};
const [form] = Form.useForm();
const [formData, setFormData] = useState({});
useDataFormObserve(formData);

return (
    <>
    <Typography.Title level={2}>演示</Typography.Title>
    <Form
        onValuesChange={(changedValues, allValues) => {
        setFormData(allValues);
        }}
        form={form}
        name="complex-form"
        onFinish={onFinish}
        labelCol={{
        span: 8,
        }}
        wrapperCol={{
        span: 16,
        }}
    >
        <Form.Item label="Username" tooltip="adf">
        <Space>
            <Form.Item
            name="username"
            noStyle
            rules={[
                {
                required: true,
                message: "Username is required",
                },
            ]}
            >
            <Input
                style={{
                width: 160,
                }}
                placeholder="Please input"
            />
            </Form.Item>
        </Space>
        </Form.Item>
        <Form.Item label="Address">
        <Input.Group>
            <Form.Item
            name={["address", "province"]}
            noStyle
            rules={[
                {
                required: true,
                message: "Province is required",
                },
            ]}
            >
            <Select placeholder="Select province">
                <Option value="Zhejiang">Zhejiang</Option>
                <Option value="Jiangsu">Jiangsu</Option>
            </Select>
            </Form.Item>
            <Form.Item
            name={["address", "street"]}
            noStyle
            rules={[
                {
                required: true,
                message: "Street is required",
                },
            ]}
            >
            <Input
                style={{
                width: "50%",
                }}
                placeholder="Input street"
            />
            </Form.Item>
        </Input.Group>
        </Form.Item>
        <Form.Item
        label="BirthDate"
        style={{
            marginBottom: 0,
        }}
        >
        <Form.Item
            name="year"
            rules={[
            {
                required: true,
            },
            ]}
            style={{
            display: "inline-block",
            width: "calc(50% - 8px)",
            }}
        >
            <Input placeholder="Input birth year" />
        </Form.Item>
        <Form.Item
            name="month"
            rules={[
            {
                required: true,
            },
            ]}
            style={{
            display: "inline-block",
            width: "calc(50% - 8px)",
            margin: "0 8px",
            }}
        >
            <Input placeholder="Input birth month" />
        </Form.Item>
        </Form.Item>
        <Form.Item label=" " colon={false}>
        <Button type="primary" htmlType="submit">
            Submit
        </Button>
        </Form.Item>
    </Form>
    </>
);
};
export default App;
                     `}
        </Highlight>
      )}

      {props.frame === "vue" && (
        <Highlight
          className=" h-52 overflow-x-hidden rounded-lg"
          style={dark}
          language="javascript"
        >
          {`<template>
    <Form :label-col="labelCol" :wrapper-col="wrapperCol">
        <FormItem label="Activity name" required>
            <Input v-model:value="modelRef.name" />
        </FormItem>
        <FormItem label="Activity zone" required>
            <Select v-model:value="modelRef.region" placeholder="please select your zone">
                <SelectOption value="shanghai">Zone one</SelectOption>
                <SelectOption value="beijing">Zone two</SelectOption>
            </Select>
        </FormItem>
        <FormItem label="Activity type" required>
            <CheckboxGroup v-model:value="modelRef.type">
                <Checkbox value="1" name="type">Online</Checkbox>
                <Checkbox value="2" name="type">Promotion</Checkbox>
                <Checkbox value="3" name="type">Offline</Checkbox>
            </CheckboxGroup>
        </FormItem>
        <FormItem class="error-infos" :wrapper-col="{ span: 14, offset: 4 }" v-bind="errorInfos">
            <Button type="primary" @click.prevent="onSubmit">Create</Button>
            <Button style="margin-left: 10px" @click="resetFields">Reset</Button>
        </FormItem>
    </Form>
</template>
<script setup>
import { reactive, toRaw, computed } from 'vue';
import { toArray } from 'lodash-es';
import { Form, Typography, FormItem, Input, Button, Select, SelectOption, CheckboxGroup, Checkbox } from 'ant-design-vue';
import useDataFormObserve from '@/hooks/useVueDataFormObserve';
const { Title } = Typography
const useForm = Form.useForm;
const labelCol = { span: 4 };
const wrapperCol = { span: 14 };
const modelRef = reactive({

});
useDataFormObserve(modelRef)
const rulesRef = reactive({
    name: [
        {
            required: true,
            message: 'Please input name',
        },
    ],
    region: [
        {
            required: true,
            message: 'Please select region',
        },
    ],
    type: [
        {
            required: true,
            message: 'Please select type',
            type: 'array',
        },
    ],
});


const { resetFields, validate, validateInfos, mergeValidateInfo } = useForm(modelRef, rulesRef);
const onSubmit = () => {
    validate()
        .then(() => {
            console.log(toRaw(modelRef));
        })
        .catch(err => {
            console.log('error', err);
        });
};
const errorInfos = computed(() => {
    return mergeValidateInfo(toArray(validateInfos));
});
</script>
<style scoped>
.error-infos :deep(.ant-form-explain) {
    white-space: pre-line;
}
</style>`}
        </Highlight>
      )}
    </div>
  );
};

export default Code;
